<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\room;


use app\common\enum\DeviceEnum;
use app\common\model\BaseModel;
use app\common\model\Building;
use app\common\model\Campus;
use app\common\model\Device;


/**
 * 公寓设备关联模型
 * Class RoomApartmentDevice
 * @package app\common\model\room
 */
class RoomApartmentDevice extends BaseModel {

    protected $name = 'room_apartment_device';

    public function building() {
        return $this->hasOne(Building::class, 'id', 'bid');
    }

    public function campus() {
        return $this->hasOne(Campus::class, 'id', 'cid');
    }

    public function room() {
        return $this->hasOne(RoomApartment::class, 'id', 'rid')->with([
            "building", "user",
        ]);
    }

    public function device() {
        return $this->hasOne(Device::class, 'id', 'device_id')->append(["device_type"]);
    }

    public function getSortAttr($value, $item) {
        return $this->handle_sort($item['sort']);
    }

    public function getTotalFeeAttr($value, $item) {
        return 1 * sprintf("%.2f", $value);
    }

    public function getCbrIdsAttr($value, $data) {
//        $bid = RoomHospital::where("id", $data['rid'])->value("bid");
        return [(int)$data['cid'], (int)$data['bid'], (int)$data['rid']];
    }

    public function getCdIdsAttr($value, $data) {
        return [(int)$data['cid'], (int)$data['device_id']];
    }

    public function getDeviceTypeTextAttr($value, $data) {
        return DeviceEnum::getDisableDesc($data['device_type']);
    }

    public function getCopyAddAttr($value, $data) {
        // 禁用append
        return "";
    }

}
